require 'watir'
include Watir
require 'test/unit'
require '../watir.config.rb'
require '../resources/functions.rb'


class TC_ManageArticles_test < Test::Unit::TestCase
   
    def startup
     
      $Name1 = 'Article1'
      $Headlines1 = 'This is the first article'
      $Post1 = 'This is the first article and here we are testing the functionality to write articles in CS 2008 '
      $Name1_Edit = 'Article1Edit'
      $Headlines1_Edit = 'Editing the headlines'
      $Post1_Edit = 'Here we are testing the functionality to edit articles in CS 2008 '
      @@ie = Watir::IE.start($SITE_URL)
    end
    
    def ie
      if defined? @@ie
        @@ie
      else
        startup
      end
    end
    
    def test_01_verifyHomeText
        assert(ie.contains_text(/Community Server/))		
    end
      
    def test_02_verifyLoginLinksExist
      if !(ie.link(:text, 'Sign in').exists?)
        ie.goto($SITE_URL + 'logout.aspx')
        ie.refresh
        ie.goto($SITE_URL)
        ie.refresh
      end
	  end
    
    def test_03_navigateToLogin
        ie.link(:text, 'Sign in').click
        assert(ie.contains_text("Sign in name"))
    end
	  
	  def test_04_populateLoginForm
        ie.text_field(:id, /username$/).value = $ADMIN_USER
        assert(ie.text_field(:id, /username$/).verify_contains($ADMIN_USER))
      
        ie.text_field(:id, /password$/).value = $ADMIN_PASSWORD
        assert(ie.text_field(:id, /password$/).verify_contains($ADMIN_PASSWORD))
	  end
	  
   def test_05_submitLoginForm
        if(ie.button(:id, /loginButton$/).exists?)
          ie.button(:id, /loginButton$/).click
        else
        ie.link(:id, /loginButton$/).click
        end
        test_01_verifyHomeText
      end
    
    def test_06_verifyControlPanel
        assert(ie.link(:text, /Control Panel/).exists?)
        ie.link(:text, /Control Panel/).click
        assert(ie.contains_text(/Announcements/))
        assert(ie.link(:text, /System Administration/).exists?)
        ie.link(:text, /System Administration/).click
        assert(ie.contains_text(/Announcements/))
    end
    
    def test_07_verifyControlPanelLinks
        assert_equal $SITE_URL + 'controlpanel/', ie.link(:text, 'Dashboard').href
        assert_equal $SITE_URL + 'controlpanel/setup.aspx', ie.link(:text, 'Administration').href
        assert_equal $SITE_URL + 'controlpanel/BlogAdmin/', ie.link(:text, 'Blog Administration').href
        assert_equal $SITE_URL + 'controlpanel/MediaGalleryAdmin/', ie.link(:text, 'Media Galleries Administration').href
        assert_equal $SITE_URL + 'controlpanel/Forums/', ie.link(:text, 'Forum Administration').href
        assert_equal $SITE_URL + 'controlpanel/HubAdmin/', ie.link(:text, 'Group Administration').href
        assert_equal $SITE_URL + 'controlpanel/Membership/membersearch.aspx?SelectedNavItem=BrowseMembers', ie.link(:text, 'Membership Administration').href
        assert_equal $SITE_URL + 'controlpanel/Settings/default.aspx', ie.link(:text, 'Site Administration').href
    end
    
    def test_08_navigateToSiteAdministration
        ie.goto($SITE_URL + 'controlpanel/Settings/default.aspx')
        assert(ie.contains_text('Site Administration'))      
    end
    
    def test_09_verifyLinksExist
        assert(ie.link(:text, 'Log Out').exists?)
        assert(ie.link(:text, 'Help').exists?)
        assert(ie.link(:text, 'Go to Site').exists?)
        assert(ie.link(:text, 'Dashboard').exists?)
        assert(ie.link(:text, 'Administration').exists?)
        assert(ie.link(:text, 'Setup').exists?)
        assert(ie.link(:text, 'Manage Licenses').exists?)
        assert(ie.link(:text, 'Telligent').exists?)
      end
      
    def test_10_navigateToCreateArticles
        ie.goto($SITE_URL + 'controlpanel/tools/ManageArticles.aspx')
        assert(ie.link(:text,'Create new article').exists?)
        ie.link(:text,'Create new article').click
        sleep 5
        ie.text_field(:id,/name$/).value=$Name1
        sleep 3
        ie.text_field(:id,/headline$/).value=$Headlines1
        sleep 5
        ie.ie.Document.ParentWindow.execscript("tinyMCE.get('ctl00_bcr_ctl01_ctl04_content_ctl00_Editor').execCommand('mceSetContent', false,'"+$Post1+"')")
        sleep 5
        assert(ie.button(:name,/SaveButton$/).exists?)
        sleep 5
        ie.button(:name,/SaveButton$/).click
        sleep 5
      end 
      
    def test_11_ViewEditArticle
        
        ie.goto($SITE_URL+'controlpanel/tools/ManageArticles.aspx')
        sleep 4
        assert(ie.table(:index,6).exists?)
        table1 = ie.table(:index,6)
        rowcount = table1.row_count
        rowcount1 =1
        ok = 0
        while rowcount1<=rowcount
            if (table1[rowcount1][2].text == $Name1)
               sleep 5
               table1[rowcount1][3].link(:text, 'Edit').click
               sleep 5
               break
             end   
         rowcount1=rowcount1+1
       end
        sleep 3
        ie.text_field(:id,/name$/).value= $Name1_Edit
        sleep 2
        ie.text_field(:id,/headline$/).value= $Headlines1_Edit
        sleep 5
        ie.ie.Document.ParentWindow.execscript("tinyMCE.get('ctl00_bcr_ctl01_ctl04_content_ctl00_Editor').execCommand('mceSetContent', false,'"+$Post1_Edit+"')")
        sleep 3
        assert(ie.button(:name,/SaveButton$/).exists?)
        sleep 4
        ie.button(:name,/SaveButton$/).click
        sleep 4
      end
      
      
    def test_12_deleteArticle
        ie.goto($SITE_URL+'controlpanel/tools/ManageArticles.aspx')
        sleep 4
        assert(ie.table(:index,6).exists?)
        table1 = ie.table(:index,6)
        rowcount = table1.row_count
        rowcount1 =1
        ok = 0
        while rowcount1<=rowcount
            if (table1[rowcount1][2].text == $Name1_Edit)
               sleep 5
               table1[rowcount1][3].link(:text, 'Edit').click
               sleep 3
               break
             end   
         rowcount1=rowcount1+1
       end
       sleep 5
       assert(ie.button(:id,/DeleteButton$/).exists?)
       sleep 3
       ie.button(:id,/DeleteButton$/).click
       sleep 2
     end
     
     
    def test_99_closeIE
        ie.close
      end  
      
end      
  